package com.fira.system.service.impl;

import java.util.Iterator;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.fira.common.core.domain.platform.dto.SysDictDataDTO;
import com.fira.common.core.domain.platform.vo.SysDictDataVO;
import com.fira.common.core.domain.platform.vo.SysDictTypeVO;
import com.fira.common.utils.DateUtils;
import com.fira.common.utils.StringUtils;
import com.fira.common.utils.uuid.IdUtils;
import com.fira.platform.service.ISysDictDataService;
import com.fira.platform.service.ISysDictTypeService;
import com.fira.system.domain.dto.SysNoticeMessageDTO;
import com.fira.system.domain.vo.SysNoticeTypeCountVO;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fira.system.mapper.SysNoticeMessageMapper;
import com.fira.system.domain.SysNoticeMessage;
import com.fira.system.service.ISysNoticeMessageService;
import com.fira.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 系统通知消息Service业务层处理
 * 
 * @author zhushuyong
 * @date 2021-10-26
 */
@Service
public class SysNoticeMessageServiceImpl implements ISysNoticeMessageService 
{
    @Autowired
    private SysNoticeMessageMapper sysNoticeMessageMapper;
    @Autowired
    private ISysDictTypeService sysDictTypeService;
    @Autowired
    private ISysDictDataService sysDictDataService;

    private static final String MESSAGE_TYPE = "message_template";
    private static final String NOTICE_TYPE = "上课通知";

    /**
     * 查询系统通知消息
     * 
     * @param id 系统通知消息ID
     * @return 系统通知消息
     */
    @Override
    public SysNoticeMessage selectSysNoticeMessageById(String id)
    {
        return sysNoticeMessageMapper.selectSysNoticeMessageById(id);
    }

    /**
     * 查询系统通知消息列表
     * 
     * @param sysNoticeMessage 系统通知消息
     * @return 系统通知消息
     */
    @Override
    public List<SysNoticeMessage> selectSysNoticeMessageList(SysNoticeMessage sysNoticeMessage)
    {
        return sysNoticeMessageMapper.selectSysNoticeMessageList(sysNoticeMessage);
    }

    /**
     * 新增系统通知消息
     * 
     * @param sysNoticeMessage 系统通知消息
     * @return 结果
     */
    @Override
    public int insertSysNoticeMessage(SysNoticeMessage sysNoticeMessage)
    {
        sysNoticeMessage.setSendTime(DateUtils.getNowDate());
        sysNoticeMessage.setCreateTime(DateUtils.getNowDate());
        return sysNoticeMessageMapper.insertSysNoticeMessage(sysNoticeMessage);
    }

    /**
     * 修改系统通知消息
     * 
     * @param sysNoticeMessage 系统通知消息
     * @return 结果
     */
    @Override
    public int updateSysNoticeMessage(SysNoticeMessage sysNoticeMessage)
    {
        sysNoticeMessage.setUpdateTime(DateUtils.getNowDate());
        return sysNoticeMessageMapper.updateSysNoticeMessage(sysNoticeMessage);
    }

    /**
     * 删除系统通知消息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysNoticeMessageByIds(String ids)
    {
        return sysNoticeMessageMapper.deleteSysNoticeMessageByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除系统通知消息信息
     * 
     * @param id 系统通知消息ID
     * @return 结果
     */
    @Override
    public int deleteSysNoticeMessageById(String id)
    {
        return sysNoticeMessageMapper.deleteSysNoticeMessageById(id);
    }

    /**
     * 批量新增通知消息
     *
     * @param sysNoticeMessageDTO
     * @return
     */
    @Override
    @Transactional
    public int addMessage(SysNoticeMessageDTO sysNoticeMessageDTO) {
        /**
         * 判断班级ID是否为空，若为空则为单条记录发送，否则为批量发送
         */
        return addNoticeMessage(sysNoticeMessageDTO);
    }

    /**
     * 查询通知消息分类及统计数量
     * @param parentId
     * @return
     */
    @Override
    public List<SysNoticeTypeCountVO> selectNoticeTypeCount(String parentId) {
        return sysNoticeMessageMapper.selectNoticeTypeCount(parentId);
    }

    /**
     * 新增通知消息
     * @param sysNoticeMessageDTO
     */
    private int addNoticeMessage(SysNoticeMessageDTO sysNoticeMessageDTO){
        SysNoticeMessage sysNoticeMessage = new SysNoticeMessage();
        sysNoticeMessage.setId(IdUtils.fastUUID());
        sysNoticeMessage.setTenantId(sysNoticeMessageDTO.getTenantId());
        sysNoticeMessage.setClassId(StringUtils.isNotEmpty(sysNoticeMessageDTO.getClassId()) ? sysNoticeMessageDTO.getClassId() : "");
        sysNoticeMessage.setReceiverId(sysNoticeMessageDTO.getReceiverId());
        sysNoticeMessage.setNoticeType(NOTICE_TYPE);
        sysNoticeMessage.setSendTime(DateUtils.getNowDate());
        sysNoticeMessage.setSenderId(sysNoticeMessageDTO.getSenderId());
        sysNoticeMessage.setTitle(sysNoticeMessageDTO.getClassName()+NOTICE_TYPE);
        SysDictTypeVO sysDictTypeVO = sysDictTypeService.selectDictTypeByType(MESSAGE_TYPE);
        if(sysDictTypeVO == null){
            return 0;
        }
        SysDictDataDTO sysDictDataDTO = new SysDictDataDTO();
        sysDictDataDTO.setDictType(sysDictTypeVO.getDictType());
        sysDictDataDTO.setDictLabel(NOTICE_TYPE);
        SysDictDataVO sysDictDataVO = sysDictDataService.selectDictDataList(sysDictDataDTO).get(0);
        if(sysDictDataVO == null){
            return 0;
        }
        String templateContent = sysDictDataVO.getDictValue();
        JSONObject jsonObject = getContentJSON(sysNoticeMessageDTO);
        for(Iterator<String> keyIt = jsonObject.keySet().iterator(); keyIt.hasNext();){
            String key = keyIt.next();
            templateContent = templateContent.replace("{{"+key+"}}",jsonObject.getString(key));
        }
        sysNoticeMessage.setMessageContent(templateContent);
        sysNoticeMessage.setCreateTime(DateUtils.getNowDate());
        return sysNoticeMessageMapper.insertSysNoticeMessage(sysNoticeMessage);
    }

    private JSONObject getContentJSON(SysNoticeMessageDTO sysNoticeMessageDTO){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("toclassDate",sysNoticeMessageDTO.getToclassDate());
        jsonObject.put("courseName",sysNoticeMessageDTO.getCourseName());
        jsonObject.put("studentName",sysNoticeMessageDTO.getStudentName());
        jsonObject.put("className",sysNoticeMessageDTO.getClassName());
        jsonObject.put("schoolName",sysNoticeMessageDTO.getSchoolName());
        return jsonObject;
    }
}
